<?php
class DictionaryAction extends CommonAction {
	
	public function del() {
		$result = false;
		$id = $_REQUEST ['id'];
		$Dictionary = M ( "Dictionary" );
		$DictionaryList = M ( "DictionaryList" );
		
		$result = $Dictionary->Where ( 'id=' . $id )->delete();
		$result = $DictionaryList->Where ( 'ITEM_FLID=' . $id )->delete();
		if ($result == false) {
			echo json_encode ( array (
					'errorMsg' => '删除出错！' 
			) );
		} else {
			echo json_encode ( array (
					'success' => true 
			) );
		}
	}
	
	public function save() {
		$result = false;
		$Dictionary = M ( "Dictionary" );
		$data ['FL_MC'] = $_REQUEST ['FL_MC'];
		$data ['FL_VALUE'] = $_REQUEST ['FL_VALUE'];
		$data ['PARENT_ID'] = $_REQUEST ['PARENT_ID'];
		$result = $Dictionary->where ( 'id=' . $_REQUEST ['id'] )->save ( $data );
		if ($result == true) {
			echo json_encode ( array (
					'success' => true 
			) );
		} else {
			echo json_encode ( array (
					'errorMsg' => '更新失败！' 
			));
		}
	}
	
	public function add() {
		$result = false;
		$Dictionary = M ( "Dictionary" );
		$data ['FL_MC'] = $_REQUEST ['FL_MC'];
		$data ['FL_VALUE'] = $_REQUEST ['FL_VALUE'];
		$data ['PARENT_ID'] = $_REQUEST ['PARENT_ID'];
		$result = $Dictionary->add ( $data );
		if ($result == true) {
			echo json_encode ( array (
					'success' => true 
			));
		} else {
			echo json_encode ( array (
					'errorMsg' => '添加出错！' 
			));
		}
	}
	
	public function delItem() {
		$result = false;
		$id = $_REQUEST ['id'];
		$DictionaryList = M ( "DictionaryList" );
		$result = $DictionaryList->Where ( 'id=' . $id )->delete();
		if ($result == false) {
			echo json_encode ( array (
					'errorMsg' => '删除出错！'
			) );
		} else {
			echo json_encode ( array (
					'success' => true
			) );
		}
	}
	
	public function addItem() {
		$result = false;
		$DictionaryList = M ( "DictionaryList" );
		$data ['ITEM_FLID'] = $_REQUEST ['ITEM_FLID'];
		$data ['ITEM_KEY'] = $_REQUEST ['ITEM_KEY'];
		$data ['ITEM_VALUE'] = $_REQUEST ['ITEM_VALUE'];
		$data ['ITEM_PARAM1'] = $_REQUEST ['ITEM_PARAM1'];
		$data ['ITEM_PARAM2'] = $_REQUEST ['ITEM_PARAM2'];
		$data ['ITEM_BZ'] = $_REQUEST ['ITEM_BZ'];
		$result = $DictionaryList->add ( $data );
		if ($result == true) {
			echo json_encode ( array (
					'success' => true
			));
		} else {
			echo json_encode ( array (
					'errorMsg' => '添加出错！'
			));
		}
	}
	
	public function saveItem() {
		$result = false;
		$DictionaryList = M ( "DictionaryList" );
		$data ['ITEM_FLID'] = $_REQUEST ['ITEM_FLID'];
		$data ['ITEM_KEY'] = $_REQUEST ['ITEM_KEY'];
		$data ['ITEM_VALUE'] = $_REQUEST ['ITEM_VALUE'];
		$data ['ITEM_PARAM1'] = $_REQUEST ['ITEM_PARAM1'];
		$data ['ITEM_PARAM2'] = $_REQUEST ['ITEM_PARAM2'];
		$data ['ITEM_BZ'] = $_REQUEST ['ITEM_BZ'];
		
		$result = $DictionaryList->where ( 'id=' . $_REQUEST ['id'] )->save ( $data );
		if ($result == true) {
			echo json_encode ( array (
					'success' => true
			) );
		} else {
			echo json_encode ( array (
					'errorMsg' => '更新失败！'
			));
		}
	}
	
	public function read() {
		$pagenum = isset ( $_POST ['page'] ) ? intval ( $_POST ['page'] ) : 1;
		$rowsnum = isset ( $_POST ['rows'] ) ? intval ( $_POST ['rows'] ) : 10;
		$dictId =  isset ( $_REQUEST ['PARENT_ID'] ) ? intval ( $_REQUEST ['PARENT_ID'] ) : "";
		$DictionaryList = M ( "DictionaryList" );
		//$Dictionary = M ( "Dictionary" );
		if($dictId != ""){
			$total = $DictionaryList->where("ITEM_FLID={$dictId}")->count (); // 计算总数
		}else{
			$total = $DictionaryList->count (); // 计算总数
		}
		
		$dictionaryListArr = array ();
		if($dictId != ""){
			$dictionaryListArr = $DictionaryList->where("ITEM_FLID={$dictId}")->limit ( ($pagenum - 1) * $rowsnum . ',' . $rowsnum )->order ( 'id asc' )->select ();
		}else{
			$dictionaryListArr = $DictionaryList ->limit ( ($pagenum - 1) * $rowsnum . ',' . $rowsnum )->order ( 'id asc' )->select ();
		}
		if ($total>0){
			$json = '{"total":' . $total . ',"rows":' . json_encode ( $dictionaryListArr ) . '}'; // 重要，easyui的标准数据格式，数据总数和数据内容在同一个json中
		}else{
			$json = '{"total":' . $total . ',"rows":' . "[]" . '}';
		}
		echo $json;
	}
	
	public function getDictionaryTree(){
		$id =  $_POST ['id'];
		$result = $this->getAsynchTreeList($id,'FL_MC','Id','PARENT_ID');
		echo json_encode($result);
	}
	
	public function getDictionaryChooseTree(){
		$result = $this->getTreeList('','FL_MC','Id','PARENT_ID');
		echo json_encode($result);
	}
}
?>
